package com.syl.web.client;

import com.syl.model.ResponseData.ArticleResponseData;
import com.syl.model.domain.Comment;
import com.syl.service.ICommentService;
import com.syl.utils.ArticleUtils;
import com.vdurmont.emoji.EmojiParser;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/comments")
public class CommentController {

  private static final Logger logger = LoggerFactory.getLogger(CommentController.class);

  @Autowired
  private ICommentService commentServcieImpl;

  // 发表评论操作
  @PostMapping(value = "/publish")
  @ResponseBody
  public ArticleResponseData publishComment(HttpServletRequest request, @RequestParam Integer aid,
      @RequestParam String text) {
    // 去除js脚本
    text = ArticleUtils.cleanXSS(text);
    text = EmojiParser.parseToAliases(text);
    // 获取当前登录用户
    User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    // 封装评论信息
    Comment comments = new Comment();
    comments.setArticleId(aid);
    comments.setIp(request.getRemoteAddr());
    comments.setCreated(new Date());
    comments.setAuthor(user.getUsername());
    comments.setContent(text);
    try {
      commentServcieImpl.pushComment(comments);
      logger.info("发布评论成功，对应博文id: " + aid);
      return ArticleResponseData.ok();
    } catch (Exception e) {
      logger.error("发布评论失败，对应博文id: " + aid + ";错误描述: " + e.getMessage());
      return ArticleResponseData.fail();
    }
  }
}

